package apptoko.com;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import apptoko.com.background.BackgroundTasks;
import apptoko.com.util.Constants;
import apptoko.com.util.Utils;

import com.androidquery.AQuery;
import apptoko.com.R;
import com.google.analytics.tracking.android.EasyTracker;

public class StartBannerActivity extends Activity implements OnClickListener{
	
	//private StartBannerView startScreenBanner;
	private Button btnCloseBanner;
	private String startScreenBannerUrl = "";
	private String slug = "";
	private String clickUrl = "";
	private AQuery aquery;
	private BackgroundTasks bgTask;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_start_banner);
		EasyTracker.getInstance().setContext(this);
		aquery = new AQuery(this);
		bgTask = new BackgroundTasks(this);
		
		//startScreenBanner = (StartBannerView) findViewById(R.id.start_screen_banner);
		btnCloseBanner = (Button) findViewById(R.id.btn_close_banner);
		btnCloseBanner.setOnClickListener(this);
		startScreenBannerUrl = getIntent().getStringExtra(Constants.AD_START_SCREEN_URL);
		slug = getIntent().getStringExtra(Constants.DISPLAY_START_BANNER);
		clickUrl = getIntent().getStringExtra(Constants.BANNER_CLICK_URL);
		aquery.id(R.id.start_screen_banner).clicked(this);
		if(TextUtils.isEmpty(startScreenBannerUrl)){
			aquery.id(R.id.start_screen_banner).gone();
			btnCloseBanner.setVisibility(View.GONE);
		} else {
			aquery.id(R.id.start_screen_banner).image(startScreenBannerUrl, false, true, 0, 0, null, AQuery.FADE_IN);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_close_banner:
			Intent i = new Intent(StartBannerActivity.this, NewsFeedActivity.class);
			startActivity(i);
			finish();
			break;
		case R.id.start_screen_banner:
			EasyTracker.getTracker().sendEvent(Constants.EVENT, Constants.EVENT_ACTION, "start_screen_banner", Long.valueOf(10));
			if(clickUrl != null || !TextUtils.isEmpty(clickUrl)){
				bgTask.clickBanner(clickUrl);
			}
			if(slug.startsWith("market")||slug.startsWith("http")){
				try {
				    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(slug)));
				} catch (android.content.ActivityNotFoundException anfe) {
				}
			}else{
				Intent intent = new Intent(StartBannerActivity.this, ItemBrokerActivity.class);
				intent.putExtra(Constants.PREVIOUS_SCREEN, Utils.getStaticCurrentStore(this) + "/StartScreenBanner");
				intent.putExtra(Constants.SLUG, slug);
				intent.putExtra(Constants.FROM, 10);
				intent.putExtra(Constants.KEY_REFERER, "ads");
				intent.putExtra(Constants.STORE_KEY, "apps");
				startActivity(intent);
				finish();
			}
			
			break;
		}
	}
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		EasyTracker.getInstance().activityStart(this);
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		EasyTracker.getInstance().activityStop(this);
	}

//	@Override
//	public void onPhotoTap(View view, float x, float y) {
//		// TODO Auto-generated method stub
//		EasyTracker.getTracker().sendEvent(Constants.EVENT, Constants.EVENT_ACTION, "start_screen_banner", Long.valueOf(10));
//		if(clickUrl != null || !TextUtils.isEmpty(clickUrl)){
//			bgTask.clickBanner(clickUrl);
//			Intent intent = new Intent(StartBannerActivity.this, ItemBrokerActivity.class);
//			intent.putExtra(Constants.PREVIOUS_SCREEN, Utils.getStaticCurrentStore(this) + "/StartScreenBanner");
//			intent.putExtra(Constants.SLUG, slug);
//			intent.putExtra(Constants.FROM, 1);
//			intent.putExtra(Constants.KEY_REFERER, "ads");
//			startActivity(intent);
//			finish();
//		}
//	}
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		Intent i = new Intent(StartBannerActivity.this, NewsFeedActivity.class);
		startActivity(i);
		finish();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
}
